package com.unswift.window.extend;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.UIManager;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class TabbedUI extends BasicTabbedPaneUI {

	// 不知道是个什么东西，绘制pane时，计算坐标和宽高的方法需要用到，直接从父类拷贝过来的
	static{
		UIManager.getDefaults().put("TabbedPane.contentBorderInsets", new Insets(0,0,0,0));
		UIManager.getDefaults().put("TabbedPane.tabsOverlapBorder", true);
	}
	
	
	private boolean tabsOverlapBorder = UIManager.getBoolean("TabbedPane.tabsOverlapBorder");

	// 边框和背景的颜色
	private Color SELECT_COLOR = new Color(187, 187, 187);


	// 绘制tab页签的边框
	@Override
	protected void paintTabBorder(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h,
			boolean isSelected) {
		// 测试了一下，发现没有边框更好看一点儿
		g.setColor(SELECT_COLOR);
		if (isSelected) {
			g.drawLine(x + w, y - 1, x + w, y + h - 2);
		} else {
			g.drawLine(x + w - 3, y-3, x + w - 3, y + h - 1);
		}
	}

	// 绘制选中的选项卡背景色
	@Override
	protected void paintTabBackground(Graphics g, int tabPlacement, int tabIndex, int x, int y, int w, int h,
			boolean isSelected) {
		Graphics2D g2d = (Graphics2D) g;
		if (isSelected) {
			g2d.setColor(Color.WHITE);
			g2d.fillRect(x, y - 1, w, h);
		} else {
			g2d.setColor(new Color(248, 248, 248));
			g2d.fillRect(x, y - 1, w, h);
		}
	}
	
	@Override
	protected void paintText(Graphics g, int tabPlacement, Font font, FontMetrics metrics, int tabIndex, String title,
			Rectangle textRect, boolean isSelected) {
		if(!isSelected){
			textRect.setLocation(textRect.x-1, textRect.y-2);
		}
		super.paintText(g, tabPlacement, font, metrics, tabIndex, title, textRect, isSelected);
	}
	
	// 绘制TabbedPane容器的四周边框样式
	@Override
	protected void paintContentBorder(Graphics g, int tabPlacement, int selectedIndex) {
		// 如果不想要边框，直接重写一个空方法
		// super.paintContentBorder(g, tabPlacement, selectedIndex);

		// 这些计算坐标和宽高的代码，直接从父类拷贝出来重用即可
		int width = tabPane.getWidth();
		int height = tabPane.getHeight();
		Insets insets = tabPane.getInsets();
		Insets tabAreaInsets = getTabAreaInsets(tabPlacement);

		int x = insets.left;
		int y = insets.top;
		int w = width - insets.right - insets.left;
		int h = height - insets.top - insets.bottom;

		y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);
		if (tabsOverlapBorder) {
			y -= tabAreaInsets.bottom;
		}
		h -= (y - insets.top);

		// 四个边框的绘制方法，都自己重写一遍，方便控制颜色和一些特效
		paintContentBorderTopEdge(g, tabPlacement, selectedIndex, x, y, w, h);
		paintContentBorderLeftEdge(g, tabPlacement, selectedIndex, x, y, w, h);
		paintContentBorderBottomEdge(g, tabPlacement, selectedIndex, x, y, w, h);
		paintContentBorderRightEdge(g, tabPlacement, selectedIndex, x, y, w, h);
	}
	
	// 绘制容器左侧边框，不是tab，是pane
	// 上下左右，都可以重写方法来绘制，相应的方法：paintContentBorder*Edge()，由paintContentBorder()方法统一调用
	@Override
	protected void paintContentBorderLeftEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w,
			int h) {
	}

	@Override
	protected void paintContentBorderTopEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w,
			int h) {
		g.setColor(SELECT_COLOR);
		g.drawLine(x, y, x + w - 2, y);
	}

	// 右边和下边，这两个需要注意，x + w 和 y + h 已达到边框临界，必须减掉几个数值，否则边框会显示不出来
	@Override
	protected void paintContentBorderRightEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w,
			int h) {
	}

	@Override
	protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement, int selectedIndex, int x, int y, int w,
			int h) {
		g.setColor(SELECT_COLOR);
		g.drawLine(x, y + h - 1, x + w - 1, y + h - 1);
	}

	// 绘制选中某个Tab后，获得焦点的样式
	@Override
	protected void paintFocusIndicator(Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
			Rectangle iconRect, Rectangle textRect, boolean isSelected) {
		// 重写空方法，主要用来去掉虚线
	}

	// 计算tab页签的宽度
	@Override
	protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
		// 可根据placement来做不同的调整
		return super.calculateTabWidth(tabPlacement, tabIndex, metrics);
	}

	// 计算tab页签的高度
	@Override
	protected int calculateTabHeight(int tabPlacement, int tabIndex, int fontHeight) {
		// 可根据placement来做不同的调整
		return super.calculateTabHeight(tabPlacement, tabIndex, fontHeight);
	}

}
